<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>XMLHttpRequest2.0 管理表单数据</title>
</head>
<body>
<form action="">
    <label for="">
        姓名: <input type="text" name="name">
    </label>

    <label for="">
        年龄: <input type="text" name="age">
    </label>
    <label for="">
        <input type="button" value="保存">
    </label>
</form>

<script>
    var btn = document.querySelector('[type=button]');

    btn.onclick = function(){

        //获取form表单
        var form = document.querySelector("form");

        //实例化FormData时将表单传递过去
        var fromData = new FormData(form);

        var xhq = new XMLHttpRequest();
        xhq.open("post","04.php");
        xhq.send(fromData);
        xhq.onreadystatechange = function(){
            if(xhq.readyState == 4 && xhq.status == 200 ){
                console.log(xhq.responseText);
            }
        }
    }
</script>
</body>
</html>